package net.risesoft.risebi.collection.web.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.risesoft.risebi.collection.model.ReportPeriod;
import net.risesoft.risebi.collection.model.ReportPeriodType;
import net.risesoft.risebi.collection.service.ReportPeriodManager;
import net.risesoft.risebi.collection.service.ReportPeriodTypeManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.GUID;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;


/**
 * indication action
 * 本类提供报告期类型管理Action
 * @author <a href="mailto:gf7@163.com">关锋</a>
 * @version $Revision$
 * 
 */
@Namespace("/admin/ReportPeriodAction")
public class ReportPeriodAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;

	private String treeId = "reportperiod-tree";
	
	private ReportPeriod reportPeriod;
	
	private List<ReportPeriodType> reportPeriodTypeList;
	
	private ReportPeriodManager reportPeriodManager;
	
	private ReportPeriodTypeManager reportPeriodTypeManager;
	
	List<ReportPeriod> allFirstReport ;
	
	private String jsonReport;
	
	private String previous_term;
	
	private String parent_term;
	
	private String lastyear_term;
	
	private String tabId;
	
	private String _dc;
	
	private String typeId;
	
	private String showMsg="保持成功！";
	
	private String targetObj="window.parent";
	
	private String reportPeriodTypeCodes;
	
	private boolean createChildInstance;
	
	private String actionType;
	

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public String getTargetObj() {
		return targetObj;
	}

	public void setTargetObj(String targetObj) {
		this.targetObj = targetObj;
	}

	public String getShowMsg() {
		return showMsg;
	}

	public void setShowMsg(String showMsg) {
		this.showMsg = showMsg;
	}

	public boolean isCreateChildInstance() {
		return createChildInstance;
	}

	public void setCreateChildInstance(boolean createChildInstance) {
		this.createChildInstance = createChildInstance;
	}

	public String getReportPeriodTypeCodes() {
		return reportPeriodTypeCodes;
	}

	public void setReportPeriodTypeCodes(String reportPeriodTypeCodes) {
		this.reportPeriodTypeCodes = reportPeriodTypeCodes;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getJsonReport() {
		return jsonReport;
	}

	public void setJsonReport(String jsonReport) {
		this.jsonReport = jsonReport;
	}

	public String getPrevious_term() {
		return previous_term;
	}

	public void setPrevious_term(String previous_term) {
		this.previous_term = previous_term;
	}

	public String getParent_term() {
		return parent_term;
	}

	public void setParent_term(String parent_term) {
		this.parent_term = parent_term;
	}

	public String getLastyear_term() {
		return lastyear_term;
	}

	public void setLastyear_term(String lastyear_term) {
		this.lastyear_term = lastyear_term;
	}

	public String getTabId() {
		return tabId;
	}

	public void setTabId(String tabId) {
		this.tabId = tabId;
	}

	public String getTreeId() {
		return treeId;
	}
	
	public String setTreeId(String treeId) {
		return treeId;
	}
	
	public List<ReportPeriodType> getReportPeriodTypeList() {
		return reportPeriodTypeList;
	}

	public void setReportPeriodTypeList(List<ReportPeriodType> reportPeriodTypeList) {
		this.reportPeriodTypeList = reportPeriodTypeList;
	}
	
	public List<ReportPeriod> getAllFirstReport() {
		return allFirstReport;
	}

	public void setAllFirstReport(
			List<ReportPeriod> allFirstReport) {
		this.allFirstReport = allFirstReport;
	}

	public ReportPeriodTypeManager getReportPeriodTypeManager() {
		return reportPeriodTypeManager;
	}

	public void setReportPeriodTypeManager(
			ReportPeriodTypeManager reportPeriodTypeManager) {
		this.reportPeriodTypeManager = reportPeriodTypeManager;
	}

	public ReportPeriod getReportPeriod() {
		return reportPeriod;
	}

	public void setReportPeriod(ReportPeriod reportPeriod) {
		this.reportPeriod = reportPeriod;
	}

	public ReportPeriodManager getReportPeriodManager() {
		return reportPeriodManager;
	}

	public void setReportPeriodManager(
			ReportPeriodManager reportPeriodManager) {
		this.reportPeriodManager = reportPeriodManager;
	}

	

	@Action(value="list",
			results={@Result(name="success", location="/admin/reportperiod/listreportperiod.jsp")}
	)
	public String list(){
		
		return SUCCESS;
	}
	
	public void getList() throws Exception{
		Map allFirstReport = reportPeriodManager.getReportPeriod(typeId,this.active_node_id,start,limit);
		
		jsonReport=getJsonString("code,guid,name,timetype,parent_term_guid,begin_time,end_time,_is_leaf",(List)allFirstReport.get("list"));
		String str="{\"success\":true,\"results\":"+(String)allFirstReport.get("count")+ ",\"rows\":"+jsonReport+"}";
		ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
		System.out.println(str);
		ServletActionContext.getResponse().getWriter().print(str);
		//return "list";
	}

	@Action(value="viewInstance",
			results={@Result(name="success", location="/admin/reportperiod/viewreportperiod.jsp")}
	)
	public String viewInstance() throws Exception {
		//获取报告期类型根节点类型；
		reportPeriodTypeList=this.reportPeriodTypeManager.getFirstReportPeriodType();
		return SUCCESS;
	}

	@Action(value="prepareAdd",
			results={@Result(name="success", location="/admin/reportperiod/addreportperiod.jsp")}
	)
	public String prepareAdd() throws Exception {
		ReportPeriodType reportPeriodType=(ReportPeriodType)this.reportPeriodManager.getObject(ReportPeriodType.class, tabId);
		reportPeriod=new ReportPeriod();
		reportPeriod.setReportPeriodType(reportPeriodType);
		reportPeriodTypeCodes=reportPeriodManager.getReportPeriodTypeCodesByPid(tabId);
		return SUCCESS;
	}
	
	@Action(value="prepareEdit",
			results={@Result(name="success", location="/admin/reportperiod/editreportperiod.jsp")}
	)
	public String prepareEdit() throws Exception {
		
		reportPeriod = (ReportPeriod)this.reportPeriodManager.getObject(ReportPeriod.class, reportPeriod.getGuid());
		return SUCCESS;
	}
	
	@Action(value="add",
			results={@Result(name="success", location="/admin/reportperiod/finish.jsp")}
	)
	public String add() throws Exception{
		tabId=reportPeriodManager.getRootReportPeriodTypeGuid(reportPeriod.getReportPeriodType());
		String guid = new GUID().toString();
		reportPeriod.setGuid(guid);
		reportPeriodManager.saveObject(reportPeriod);
		if(isCreateChildInstance())reportPeriodManager.createChildInstance(reportPeriod);
		return SUCCESS;
	}
	
	@Action(value="edit",
			results={@Result(name="success", location="/admin/reportperiod/finish.jsp")}
	)
	public String edit(){
		tabId=reportPeriodManager.getRootReportPeriodTypeGuid(reportPeriod.getReportPeriodType());
		reportPeriodManager.saveObject(reportPeriod);
		return SUCCESS;
	}
	
	@Action(value="prepareShowList",
			results={@Result(name="success", location="/admin/application/appList.jsp")}
	)
	public String prepareShowList() throws Exception{
		return SUCCESS;
	}
	
	public void showList() throws Exception{
		String id=typeId;
		ReportPeriodType reportPeriodType=(ReportPeriodType)this.reportPeriodManager.getObject(ReportPeriodType.class, typeId);
		if(actionType.equalsIgnoreCase("parent_term")){
			if(null!=reportPeriodType.getParentReportPeriodType()){
				id=reportPeriodType.getParentReportPeriodType().getGuid();
			}else{
				id="";
			}
		}else if(actionType.equalsIgnoreCase("previous_term")){
			//上期
		}else if(actionType.equalsIgnoreCase("lastyear_term")){
			//去年同期
		}else {
			id="";
		}
		Map allFirstReport = reportPeriodManager.getReportPeriod(id,this.active_node_id,start,limit);
		
		jsonReport=getJsonString("code,guid,name,timetype,parent_term_guid,begin_time,end_time,_is_leaf",(List)allFirstReport.get("list"));
		String str="{\"success\":true,\"results\":"+(String)allFirstReport.get("count")+ ",\"rows\":"+jsonReport+"}";
		ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
		System.out.println(str);
		ServletActionContext.getResponse().getWriter().print(str);
	}
	
	@Action(value="prepareAddChildren",
			results={@Result(name="success", location="/admin/reportperiod/addreportperiodtypechildren.jsp")}
	)
	public String prepareAddChildren(){
		List list=reportPeriodManager.getObjects(ReportPeriod.class);
		return SUCCESS;
	}
	
	@Action(value="remove")
	public String remove() throws Exception{
		reportPeriod=(ReportPeriod)reportPeriodManager.getObject(ReportPeriod.class, reportPeriod.getGuid());
		tabId=reportPeriodManager.getRootReportPeriodTypeGuid(reportPeriod.getReportPeriodType());
		LazyDynaBean ldb = new  LazyDynaBean();
		//判断表示是否删除子节点
		if(null!=actionType && !actionType.equalsIgnoreCase("all")){
			//不删子节点则校验是否有子，有则不能删除
			if(reportPeriodManager.hasChild(reportPeriod.getGuid())){
				ldb.set("success", false);
				ldb.set("reason", "有子节点，不能删除！");
				this.json = JSONObject.fromObject(ldb).toString();
				return REMOVE;
			}
		}
		reportPeriodManager.removeObject(ReportPeriod.class, reportPeriod.getGuid());
		ldb.set("success", true);
		ldb.set("reason", "删除成功");
		this.json = JSONObject.fromObject(ldb).toString();
		return AJAX;
	}

	public String get_dc() {
		return _dc;
	}

	public void set_dc(String _dc) {
		this._dc = _dc;
	}

	private String getJsonString(String fields,List list) {
		StringBuffer jsonStr=new StringBuffer("[");
		String[] fieldslist=fields.split(",");
		for(int j=0;j<list.size();j++){
			Object[] obj=(Object[])list.get(j);
			if(j>0) jsonStr.append(",");
			jsonStr.append("{");
			for(int i=0;i<fieldslist.length;i++){
				if(i>0) jsonStr.append(",");
				jsonStr.append("\"").append(fieldslist[i]).append("\":").append("\"");
				if(null==obj[i]){
					jsonStr.append("");
				}else{
					if(obj[i] instanceof java.sql.Timestamp || obj[i] instanceof java.util.Date) {
						if(null != obj[i]){
							Date objDate= (Date) obj[i];
							SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
							jsonStr.append(sf.format(objDate));
						}
					}else {
							jsonStr.append(obj[i].toString());
							
					}
				}
				jsonStr.append("\"");
			}
			jsonStr.append("}");
		}
		jsonStr.append("]");
		return jsonStr.toString();
	}
}
